package com.fengwk.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.face.model.User;
import com.fengwk.user.service.RegisterService;
import com.fengwk.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "注册")
@RestController
@RequestMapping("/api/register")
public class RegisterController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private RegisterService registerService;
	@Autowired
	private UserService userService;
	
	@ApiOperation(
			value = "注册用户", 
			response = User.class)
	@PostMapping("/register")
	public void register(
			@ApiParam(value = "用户头像id", required = false) @RequestParam(value = "avatarId", required = false) Long avatarId, 
			@ApiParam(value = "用户名", required = false) @RequestParam(value = "username", required = false) String username, 
			@ApiParam(value = "邮箱", required = false) @RequestParam(value = "email", required = false) String email, 
			@ApiParam(value = "手机号", required = false) @RequestParam(value = "mobilePhone", required = false) String mobilePhone, 
			@ApiParam(value = "密码", required = false) @RequestParam(value = "password", required = false) String password, 
			@ApiParam(value = "昵称", required = false) @RequestParam(value = "nickname", required = false) String nickname,
			@ApiParam(value = "真实姓名", required = false) @RequestParam(value = "realName", required = false) String realName,
			@ApiParam(value = "身份证", required = false) @RequestParam(value = "idCard", required = false) String idCard, 
			@ApiParam(value = "性别", required = false) @RequestParam(value = "sex", required = false) Integer sex, 
			@ApiParam(value = "出生时间", required = false) @RequestParam(value = "birthTime", required = false) Long birthTime) {
		ResultUtils.returnJsonResult(request, response, registerService.register(
				avatarId, username, email, mobilePhone, password, nickname, realName, idCard, sex, birthTime));
	}
	
	@ApiOperation(
			value = "重置密码", 
			response = Void.class)
	@PostMapping("/resetPassword")
	public void resetPassword(
			@ApiParam(value = "身份", required = true) @RequestParam(value = "identity", required = true) String identity,
			@ApiParam(value = "随机码", required = false) @RequestParam(value = "random", required = false) String random,
			@ApiParam(value = "密码", required = false) @RequestParam(value = "password", required = false) String password) {
		registerService.resetPassword(identity, random, password);
		ResultUtils.returnJsonResult(request, response, true);
	}
	
	@ApiOperation(
			value = "更新用户", 
			response = User.class)
	@PostMapping("/updateUser")
	public void updateUser(
			@ApiParam(value = "用户id,更新键", required = true) @RequestParam(value = "userId", required = true) Long userId,
			@ApiParam(value = "用户头像id", required = false) @RequestParam(value = "avatarId", required = false) Long avatarId,
			@ApiParam(value = "用户名", required = false) @RequestParam(value = "username", required = false) String username,
			@ApiParam(value = "邮箱", required = false) @RequestParam(value = "email", required = false) String email,
			@ApiParam(value = "手机号", required = false) @RequestParam(value = "mobilePhone", required = false) String mobilePhone,
			@ApiParam(value = "密码", required = false) @RequestParam(value = "password", required = false) String password,
			@ApiParam(value = "昵称", required = false) @RequestParam(value = "nickname", required = false) String nickname,
			@ApiParam(value = "真实姓名", required = false) @RequestParam(value = "realName", required = false) String realName,
			@ApiParam(value = "身份证", required = false) @RequestParam(value = "idCard", required = false) String idCard,
			@ApiParam(value = "性别", required = false) @RequestParam(value = "sex", required = false) Integer sex,
			@ApiParam(value = "出生时间", required = false) @RequestParam(value = "birthTime", required = false) Long birthTime) {
		ResultUtils.returnJsonResult(request, response, userService.updateUser(
				userId, avatarId, username, email, mobilePhone, password, nickname, realName, idCard, sex, birthTime, null));
	}
	
	@ApiOperation(
			value = "检验用户名存在", 
			response = boolean.class)
	@PostMapping("/existUsername")
	public void existUsername(
			@ApiParam(value = "用户名", required = true) @RequestParam(value = "username", required = true) String username) {
		boolean exist = registerService.existUsername(username);
		ResultUtils.returnJsonResult(request, response, exist, exist ? "用户名存在" : "用户名不存在");
	}
	
	@ApiOperation(
			value = "检验邮箱存在", 
			response = boolean.class)
	@PostMapping("/existEmail")
	public void existEmail(
			@ApiParam(value = "邮箱", required = true) @RequestParam(value = "email", required = true) String email) {
		boolean exist = registerService.existEmail(email);
		ResultUtils.returnJsonResult(request, response, exist, exist ? "邮箱名存在" : "邮箱不存在");
	}
	
	@ApiOperation(
			value = "检验手机号存在", 
			response = boolean.class)
	@PostMapping("/existMobilePhone")
	public void existMobilePhone(
			@ApiParam(value = "手机号", required = true) @RequestParam(value = "mobilePhone", required = true) String mobilePhone) {
		boolean exist = registerService.existMobilePhone(mobilePhone);
		ResultUtils.returnJsonResult(request, response, exist, exist ? "手机号存在" : "手机号不存在");
	}
	
}
